package com.fhj.movie.activity;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.w3c.dom.ls.LSInput;

import com.baidu.platform.comapi.map.h;
import com.fhj.movie.entity.HyyContanctEntity;
import com.fhj.movie_fhj_01.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HyyKeFuActivity extends Activity {
	private PullToRefreshListView listView;
	private List<HyyContanctEntity> list;
	private Button send;
	private EditText editText;
	private HyyContanctEntity entity;
	private MyAdapetr adapetr;
	private TextView kefu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		setContentView(R.layout.activity_kefu);
		listView = (PullToRefreshListView) findViewById(R.id.kefu_listview);
		send = (Button) this.findViewById(R.id.contact_send);
		editText = (EditText) this.findViewById(R.id.contac_edit);
		kefu=(TextView)this.findViewById(R.id.kefu_lefu);
		list = new ArrayList<HyyContanctEntity>();
		
		adapetr = new MyAdapetr();
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String s = editText.getText().toString().trim();
				entity = new HyyContanctEntity();
				entity.setId(0);
				entity.setName(s);
				list.add(entity);
				String ss = "网络服务正忙，请稍后";
				entity = new HyyContanctEntity();
				entity.setId(1);
				entity.setName(ss);
				list.add(entity);
				adapetr.notifyDataSetChanged();
			}
		});
		kefu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("tel:020-83568090");    
            	Intent intent = new Intent(Intent.ACTION_DIAL, uri);       
            	startActivity(intent); 
            	
			}
		});
		listView.setAdapter(adapetr);
	}

	class MyAdapetr extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(HyyKeFuActivity.this)
						.inflate(R.layout.contact_item, parent, false);
				holder.kefu = (EditText) convertView
						.findViewById(R.id.contact_kefu);
				holder.client = (EditText) convertView
						.findViewById(R.id.contact_client);
				holder.layout = (LinearLayout) convertView
						.findViewById(R.id.contact_zongkefu);
				holder.relativeLayout = (RelativeLayout) convertView
						.findViewById(R.id.contact_zongclient);
				holder.time = (TextView) convertView
						.findViewById(R.id.contact_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (list.get(position).getId()==0) {
				Date currentTime = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String dateString = formatter.format(currentTime);
				holder.time.setText(dateString);
				holder.client.setText(list.get(position).getName());
				holder.layout.setVisibility(View.INVISIBLE);
				holder.relativeLayout.setVisibility(View.VISIBLE);
			}else if(list.get(position).getId()==1){
				Date currentTime = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String dateString = formatter.format(currentTime);
				holder.time.setText(dateString);
				holder.kefu.setText(list.get(position).getName());
				holder.layout.setVisibility(View.VISIBLE);
				holder.relativeLayout.setVisibility(View.INVISIBLE);
			}
			
			return convertView;
		}
	}

	class ViewHolder {
		public EditText kefu;
		public EditText client;
		public LinearLayout layout;
		public RelativeLayout relativeLayout;
		public TextView time;
	}
}
